//
//  AudioPlayEngine.m
//  51-Tour
//
//  Created by xiang ying on 14-3-26.
//  Copyright (c) 2014年 xiang ying. All rights reserved.
//

#import "AudioPlayEngine.h"

static AudioPlayEngine *engine = nil;


@interface AudioPlayEngine()<AVAudioPlayerDelegate>{
    AVAudioPlayer *_avPlayer;
}

@end

@implementation AudioPlayEngine
@synthesize _delegate;

+(instancetype)instance{
    if (!engine) {
        engine = [[self alloc] init];
    }
    return engine;
}

-(id)init{
    
    if(self = [super init]){
        
    }
    
    return self;
}

-(void)dealloc{
    [[AVAudioSession sharedInstance] setActive:NO error:nil];
}

//开始播放
-(NSTimeInterval)playAudio:(NSData*)data{
    if (!_delegate) {
        return -1;
    }
    [self stopPlay:NO];
    NSError *_error = nil;
    _avPlayer = [[AVAudioPlayer alloc] initWithData:data error:&_error];
    _avPlayer.delegate = self;
    _avPlayer.volume = 1.0;
    [_avPlayer prepareToPlay];
    [_avPlayer play];
    if (!_avPlayer) {
        return -1;
    }
    [[AVAudioSession sharedInstance] setActive:YES error:nil];
    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
    return  _avPlayer.duration;
}

-(void)play{
    if (!_delegate) {
        return;
    }
    [_avPlayer play];
}
//快进
-(BOOL)playAttime:(NSTimeInterval)time{
    if (_avPlayer) {
//        NSLog(@"time:%f,%f",time,_avPlayer.duration);
        [_avPlayer setCurrentTime:time];
        if (fabs(time-_avPlayer.duration) <0.05) {
            //误差
            [self stopPlay:NO];
            return NO;
        }
    }
    return YES;
}


//暂停播放
-(void)pausePlay{
    [_avPlayer pause];
}

//停止播放
-(void)stopPlay:(BOOL)flag{
    [[AVAudioSession sharedInstance] setActive:NO error:nil];
    [_avPlayer stop];
    [_avPlayer setCurrentTime:0];
    [self audioPlayerDidFinishPlaying:_avPlayer successfully:flag];
//    _avPlayer = nil;
}

//获取当前播放时长
-(NSTimeInterval)getCurrentPlayTime{
    return _avPlayer.currentTime;
}

-(BOOL)isPlaying{
    return _avPlayer.isPlaying;
}

-(BOOL)hasData{
    if (_avPlayer) {
        return YES;
    }
    return NO;
}
#pragma mark - AVAudioPlayerDelegate
//播放完毕
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag{
    if ([_delegate respondsToSelector:@selector(audioPlayerDidFinishPlaying:)]) {
        [_delegate  audioPlayerDidFinishPlaying:flag];
    }
}

//播放被打断
- (void)audioPlayerBeginInterruption:(AVAudioPlayer *)player{
    if ([_delegate respondsToSelector:@selector(audioPlayerBeginInterruption)]) {
        [_delegate  audioPlayerBeginInterruption];
    }
}

- (void)audioPlayerEndInterruption:(AVAudioPlayer *)player{
    if ([_delegate respondsToSelector:@selector(audioPlayerEndInterruption)]) {
        [_delegate  audioPlayerEndInterruption];
    }
}

//- (void)audioPlayerEndInterruption:(AVAudioPlayer *)player withOptions:(NSUInteger)flags{
//    if (flags) {
//        if ([_delegate respondsToSelector:@selector(audioPlayerEndInterruption)]) {
//            [_delegate  audioPlayerEndInterruption];
//        }
//    }
//}

@end
